import { requestClient } from '#/api/request';
import type { UserInfo } from './userRole';

/**
 * 用户列表查询参数
 */
export interface UserListParams {
  pageNo?: number;
  pageSize?: number;
  username?: string;
  realName?: string;
  status?: number;
  phone?: string;
}

/**
 * 角色信息
 */
export interface Role {
  id: number;
  name: string;
  roleKey: string;
  status: number;
}

/**
 * 权限信息
 */
export interface Permission {
  id: number;
  menuName: string;
  path: string;
  component: string;
  status: string;
  perms: string;
  parentId: number | null;
  children: Permission[] | null;
}

/**
 * 完整的用户信息（基于接口返回示例）
 */
export interface UserDetail {
  id: number;
  username: string;
  password: string;
  status: number;
  realName: string | null;
  idCard: string | null;
  idCardFrontUrl: string | null;
  idCardBackUrl: string | null;
  nickName: string | null;
  gender: number;
  birthday: string | null;
  avatar: string | null;
  phone: string;
  phoneVerified: number;
  lastLoginAt: string | null;
  loginCount: number;
  verificationStatus: number;
  openId: string | null;
  token: string | null;
  role: Role;
  perms: Permission[];
}

/**
 * 用户列表响应数据（基于接口返回示例）
 */
export interface UserListResult {
  pageNo: number;
  pageSize: number;
  result: UserDetail[];
  total: number;
  totalPages: number;
  start: number;
  end: number;
}

/**
 * API响应格式
 */
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

/**
 * 用户编辑表单数据
 */
export interface UserFormData {
  id?: number;
  username: string;
  password?: string;
  realName: string | null;
  nickName: string | null;
  gender: number;
  birthday: string | null | undefined;
  avatar: string | null;
  phone: string;
  status: number;
  roleId: number;
}

/**
 * 获取用户列表（分页）
 */
export async function getUserListApi(params?: UserListParams) {
  return requestClient.get<ApiResponse<UserListResult>>('/user/list', { params });
}

/**
 * 获取用户详细信息
 */
export async function getUserDetailApi(id: number) {
  return requestClient.get<ApiResponse<UserDetail>>(`/user/${id}`);
}

/**
 * 修改用户信息
 */
export async function updateUserApi(data: UserFormData) {
  return requestClient.put<ApiResponse<any>>('/user', data);
}

/**
 * 新增用户
 */
export async function createUserApi(data: UserFormData) {
  return requestClient.post<ApiResponse<any>>('/user', data);
}

/**
 * 注销/删除用户（支持批量）
 */
export async function deleteUsersApi(userIds: number[]) {
  return requestClient.delete<ApiResponse<any>>('/user', { data: userIds });
}

/**
 * 获取所有用户（不分页）
 */
export async function getAllUsersApi() {
  return requestClient.get<UserInfo[]>('/user/listAll');
} 
